package com.yanqu.road.server.manager.zhaoshang.sort;

import com.yanqu.road.entity.zhaoshang.ZsProject;
import com.yanqu.road.server.manager.zhaoshang.ZsMgr;

import java.util.Comparator;
import java.util.Map;

public class ZsSortOrderByProfit implements Comparator<ZsProjectSortEntity> {

    private long playId;

    private long serverId;

    //只按收益
    private boolean onlyProfit;

    public ZsSortOrderByProfit(long playId, long serverId, boolean onlyProfit) {
        this.playId = playId;
        this.serverId = serverId;
        this.onlyProfit = onlyProfit;
    }

    @Override
    public int compare(ZsProjectSortEntity first, ZsProjectSortEntity last) {
        Map<Long, ZsProject> map = ZsMgr.getZsProjectMapByServerId(first.getServerId());
        Map<Long, ZsProject> map2 = ZsMgr.getZsProjectMapByServerId(last.getServerId());
        if(map == null || map2 == null){
            return 0;
        }
        ZsProject firstProject = map.get(first.getProjectId());
        ZsProject lastProject = map2.get(last.getProjectId());
        if(firstProject == null || lastProject == null){
            return 0;
        }
        if(!onlyProfit) {
            int i = 0;
            if (firstProject.getOpenUserId() == playId ) {
                i--;
            }
            if (lastProject.getOpenUserId() == playId ) {
                i++;
            }
            if (i != 0) {
                return i;
            }
            for (int key : firstProject.getSeatMap().keySet()) {
                if (firstProject.getSeatMap().get(key).getUserId() == playId) {
                    i--;
                }
            }
            for (int key : lastProject.getSeatMap().keySet()) {
                if (lastProject.getSeatMap().get(key).getUserId() == playId) {
                    i++;
                }
            }
            if (i != 0) {
                return i;
            }
        }
        long firstProfit = firstProject.getExpectProfit();
        long lastProfit = lastProject.getExpectProfit();
        if(firstProfit > lastProfit){
            return -1;
        }else if(firstProfit < lastProfit){
            return 1;
        }
        return 0;
    }
}
